﻿$.postJSON = function(url, data, callback) {
    return $.post(url, data, callback, "json");
};

$.getJSON = function(url, data, callback) {
    return $.get(url, data, callback, "json");
};

$.getTranslations = function(references, callback) {
    $.postJSON("/cms/translations", { references: $.compactJSON(references) }, function(json) {
        callback(json);
    });
};

$.hasPermission = function(permission, value, callback) {
    $.postJSON("/cms/account/hasPermission", { permission: permission, value: value }, function(r) {
        callback(r);
    });
};

function Guid(options) {
    this.options = options || {};
    this.chars = this.options.chars || Guid.constants.alphanumerics;
    this.epoch = this.options.epoch || Guid.constants.epoch1970;
    this.counterSequenceLength = this.options.counterSequenceLength || 1;
    this.randomSequenceLength = this.options.randomSequenceLength || 2;
}

Guid.prototype.generate = function() {
    var now = (new Date()).getTime() - this.epoch;
    var guid = this.baseN(now);

    this.counterSeq = (now == this.lastTimestampUsed ? this.counterSeq + 1 : 1);
    guid += this.counterSeq;

    for (var i = 0; i < this.randomSequenceLength; i++) {
        guid += this.chars[Math.floor(Math.random() * this.chars.length)];
    }

    this.lastTimestampUsed = now;

    return guid;
}

Guid.prototype.baseN = function(val) {
    if (val == 0) return "";
    var rightMost = val % this.chars.length;
    var rightMostChar = this.chars[rightMost];
    var remaining = Math.floor(val / this.chars.length);
    return this.baseN(remaining) + rightMostChar;
}

Guid.constants = {};
Guid.constants.numbers = "0123456789";
Guid.constants.alphas = "abcdefghijklmnopqrstuvwxyz";
Guid.constants.lowerAlphanumerics = "0123456789abcdefghijklmnopqrstuvwxyz";
Guid.constants.alphanumerics = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// http://tools.ietf.org/html/rfc1924
Guid.constants.base85 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~";

Guid.constants.epoch1970 = (new Date(0));
Guid.constants.epoch = function(year) { return (new Date("Jan 1 " + year)).getTime(); }

var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad(str, len, pad, dir) {

    if (typeof (len) == "undefined") { var len = 0; }
    if (typeof (pad) == "undefined") { var pad = ' '; }
    if (typeof (dir) == "undefined") { var dir = STR_PAD_RIGHT; }

    if (len + 1 >= $(str).length) {

        switch (dir) {

            case STR_PAD_LEFT:
                str = Array(len + 1 - $(str).length).join(pad) + str;
                break;

            case STR_PAD_BOTH:
                var right = Math.ceil((padlen = len - $(str).length) / 2);
                var left = padlen - right;
                str = Array(left + 1).join(pad) + str + Array(right + 1).join(pad);
                break;

            default:
                str = str + Array(len + 1 - $(str).length).join(pad);
                break;

        } // switch

    }

    return str;

}

function padLeft(ContentToSize, PadLength, PadChar) {
    var PaddedString = ContentToSize.toString();
    for (i = ContentToSize.length + 1; i <= PadLength; i++) {
        PaddedString = PadChar + PaddedString;
    }
    return PaddedString;
}
